package com.tale.competition.common.config;

import com.tale.competition.common.constant.RabbitConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * author: 蔡奇峰
 * date: 2020/4/29 21:21
 **/
@Configuration
//@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitConfig {

//    private String virtualHost;
//    private String connectionTimeout;
//    private boolean publisherReturns;


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

//    @Autowired
//    private CachingConnectionFactory connectionFactory;


    /*@Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMessageConverter(converter());

        rabbitTemplate.setEncoding("UTF-8");

        // 消息是否成功发送到Exchange
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack) {
                logger.info("消息成功发送到Exchange");
//                String msgId = correlationData.getId();
//                msgLogService.updateStatus(msgId, Constant.MsgLogStatus.DELIVER_SUCCESS);
            } else {
                logger.info("消息发送到Exchange失败, {}, cause: {}", correlationData, cause);
            }
        });

        // 触发setReturnCallback回调必须设置mandatory=true, 否则Exchange没有找到Queue就会丢弃掉消息, 而不会触发回调
        rabbitTemplate.setMandatory(true);
        // 消息是否从Exchange路由到Queue, 注意: 这是一个失败回调, 只有消息从Exchange路由到Queue失败才会回调这个方法
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            logger.info("消息从Exchange路由到Queue失败: exchange: {}, route: {}, replyCode: {}, replyText: {}, message: {}", exchange, routingKey, replyCode, replyText, message);
        });

        return rabbitTemplate;
    }*/



//    @Bean
//    public Jackson2JsonMessageConverter converter() {
//        return new Jackson2JsonMessageConverter();
//    }


    /**
     * 获取邮箱队列
     * @return
     */
    @Bean
    public Queue mailQueue() {
        return new Queue(RabbitConstant.EMAIL_QUEUE, true); //队列持久
    }

    /**
     * 交换机
     * @return
     */
    @Bean
    public TopicExchange defaultExchange() {
        return new TopicExchange(RabbitConstant.EMAIL_EXCHANGE);
    }

    /**
     * 一个交换机可以绑定多个消息队列，也就是消息通过一个交换机，可以分发到不同的队列当中去
     * @return
     */
    @Bean
    public Binding bindingMail() {
        return BindingBuilder.bind(mailQueue()).to(defaultExchange()).with(RabbitConstant.MESSAGE_ROUTING_KEY);
    }



}
